package light.head.config;

import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import light.head.framework.entity.DBConnCfg;
import light.head.framework.utils.XmlUtils;

import org.dom4j.Document;
import org.dom4j.Element;

/**
 * @author janhang
 * @2010-12-14  下午01:44:15
 */
public class Config {
	/**
	 * 分别以Document形式和Map形式缓存配置文件
	 * 便于保存和访问配置信息
	 */
	private static Document configDoc = null;
	private static Map<String, Object> config = null;

	public static Map<String, Object> getConfig() {
		return config;
	}
	
	/**
	 * 获取配置文件Document对象
	 */
	public static Document getConfigDoc() {
		if (configDoc == null)
			loadConfigDoc();
		return configDoc;
	}
	/**
	 * 加载配置文件信息
	 * @return 是否加载成功
	 * @author 简航
	 * 创建日期 2010-03-12 16:12:21
	 */
	public static Boolean loadConfigDoc() {
		if (configDoc == null) {
			InputStream in = Config.class.getResourceAsStream("/config/config.xml");
			configDoc = XmlUtils.loadXML(in);
			if (configDoc == null) {
				System.out.println("配置文件加载失败!");
				return false;
			}
		}
		return true;
	}
	/**
	 * 加载config.xml文件
	 * @return 是否加载成功
	 * @author 简航 创建日期 2009-11-25 14:33:22
	 */
	@SuppressWarnings("unchecked")
	public static Boolean loadConfig() {
		Element element = getConfigDoc().getRootElement();
		config = new HashMap<String, Object>();
		for (Iterator<Element> pe = element.elementIterator(); pe.hasNext();) {
			Element pei = pe.next();

			if ("text".equals(pei.getName())) {
				config.put(pei.attributeValue("id"), pei.attributeValue("value"));
			}

			if ("map".equals(pei.getName())) {
				config.put(pei.attributeValue("id"), loadMap(pei));
			}

			if ("database".equals(pei.getName())) {
				config.put(pei.attributeValue("id"), loadDBConnCfg(pei));
			}
			
			if ("list".equals(pei.getName())) {
				config.put(pei.attributeValue("id"), loadList(pei));
			}
		}
		return true;
	}

	@SuppressWarnings("unchecked")
	private static List<String> loadList(Element e) {
		List<String> list = null;
		if (e != null)
			list = new ArrayList<String>();
		for (Iterator<Element> listPe = e.elementIterator(); listPe.hasNext();) {
			Element item = listPe.next();
			list.add(item.attributeValue("str"));
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	private static Object loadMap(Element e) {
		Map<String, Object> map = new HashMap<String, Object>();
		for (Iterator<Element> listPe = e.elementIterator(); listPe.hasNext();) {
			Element item = listPe.next();
			map.put(item.attributeValue("id"),
					(item.attributeValue("str") != null
							&& !"".equals(item.attributeValue("str")) ? item
							.attributeValue("str") : true));
		}

		return map;
	}

	@SuppressWarnings("unchecked")
	private static DBConnCfg loadDBConnCfg(Element e) {
		DBConnCfg dci = new DBConnCfg();
		Field fd = null;
		
		for (Iterator<Element> listPe = e.elementIterator(); listPe.hasNext();) {
			Element item = listPe.next();
			try {
				fd = DBConnCfg.class.getDeclaredField(item.attributeValue("id"));
				fd.setAccessible(true);
				fd.set(dci, item.attributeValue("str"));
			} catch (SecurityException e1) {
				e1.printStackTrace();
			} catch (NoSuchFieldException e1) {
				e1.printStackTrace();
			} catch (IllegalArgumentException e1) {
				e1.printStackTrace();
			} catch (IllegalAccessException e1) {
				e1.printStackTrace();
			}
		}
		return dci;
	}
}
